home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / disk-tools / harddisk / rdbmount_1_2 / tool.c < prev   
C/C++ Source or Header  |  1993-03-28  |  9KB  |  334 lines

  1. /* $Revision Header *** Header built automatically - do not edit! ***********
  2.  *
  3.  *    (C) Copyright 1992 by Torsten Jürgeleit
  4.  *
  5.  *    Name .....: tool.c
  6.  *    Created ..: Wednesday 19-Feb-92 10:05:27
  7.  *    Revision .: 2
  8.  *
  9.  *    Date        Author                 Comment
  10.  *    =========   ====================   ====================
  11.  *    27-Apr-92   Torsten Jürgeleit      add no library option
  12.  *    11-Mar-92   Torsten Jürgeleit      argv[ARGUMENT_UNIT] is a (BYTE *)
  13.  *    19-Feb-92   Torsten Jürgeleit      Created this file!
  14.  *
  15.  ****************************************************************************
  16.  *
  17.  *    Tool for manipulating hardblocks
  18.  *
  19.  * $Revision Header ********************************************************/
  20.  
  21. /* Includes */
  22.  
  23. #include <exec/types.h>
  24. #include <devices/hardblocks.h>
  25. #include <libraries/arpbase.h>
  26. #include <functions.h>
  27. #include <stdlib.h>
  28. #ifndef LINK_TEST
  29. #include "hardblocks.h"
  30. #else    /* LINK_TEST */
  31. #include "hardblocks_link.h"
  32. #endif    /* LINK_TEST */
  33.  
  34. /* Defines */
  35.  
  36. #define DEFAULT_DEVICE        "scsi.device"
  37. #define DEFAULT_UNIT        "0"
  38.  
  39. #define MAX_ARGUMENTS        11
  40.  
  41. #define ARGUMENT_DEVICE        0
  42. #define ARGUMENT_UNIT        1
  43. #define ARGUMENT_FILE        2
  44. #define ARGUMENT_LOAD        3
  45. #define ARGUMENT_RESTORE    4
  46. #define ARGUMENT_DEFAULT    5
  47. #define ARGUMENT_SHOW        6
  48. #define ARGUMENT_OUTPUT        7
  49. #define ARGUMENT_SAVE        8
  50. #define ARGUMENT_BACKUP        9
  51. #define ARGUMENT_REMOVE        10
  52.  
  53. /* Externals */
  54.  
  55. IMPORT struct DOSBase *DOSBase;
  56.  
  57. /* Globals */
  58.  
  59. struct ArpBase *ArpBase;
  60. #ifndef LINK_TEST
  61. struct Library *HardBlocksBase;
  62. #endif    /* LINK_TEST */
  63.  
  64. BYTE template[] = "Device,Unit,FILE/k,LOAD/s,RESTORE/s,DEFAULT/s,SHOW/s,"
  65. "OUTPUT/k,SAVE/s,BACKUP/s,REMOVE/s", xtra_help[] = "HBtool v1.1 - Copyright © 1992 Torsten Jürgeleit\n\n"
  66. "Usage: HBtool [Device] [Unit] [FILE name] [LOAD]"
  67. " [RESTORE] [DEFAULT] [SHOW] [OUTPUT file] [SAVE]"
  68. " [BACKUP] [REMOVE]\n"
  69. "\t[Device]      = device name (default: scsi.device)\n"
  70. "\t[Unit]        = device unit num (default 0)\n"
  71. "\t[FILE name]   = file to restore/save hardblocks\n"
  72. "\t[LOAD]        = load hardblocks from device (default)\n"
  73. "\t[RESTORE]     = restore hardblocks from file\n"
  74. "\t[DEFAULT]     = create standard rigid disk block\n"
  75. "\t[SHOW]        = show currently loaded hardblocks (default)\n"
  76. "\t[OUTPUT file] = output file for show (default: NULL -> stdout)\n"
  77. "\t[SAVE]        = save currently loaded hardblocks to device\n"
  78. "\t[BACKUP]      = backup currently loaded hardblocks to file\n"
  79. "\t[REMOVE]      = delete rigid disk block from device";
  80.  
  81. /* Prototypes */
  82.  
  83. LONG _main(LONG alen, BYTE * aptr);
  84. BOOL safety_check(BYTE * device, ULONG unit);
  85.  
  86. /* Main routine - no startup code */
  87.  
  88. LONG
  89. _main(LONG alen, BYTE * aptr)
  90. {
  91.     LONG return_code = RETURN_FAIL;
  92.  
  93.     /* First open ARP library */
  94.     if (!(ArpBase = OpenLibrary(ArpName, ArpVersion)))
  95.         {
  96.             Write(Output(), "Need ARP library V39+\n", 22L);
  97.         }
  98.     else
  99.         {
  100. #ifndef LINK_TEST
  101.             if (!(HardBlocksBase = OpenLibrary(HardBlocksName,
  102.                             HardBlocksVersion)))
  103.                 {
  104.                     Puts("Need hardblocks library");
  105.                 }
  106.             else
  107.                 {
  108. #endif    /* LINK_TEST */
  109.                     BYTE *argv[MAX_ARGUMENTS];
  110.                     USHORT i;
  111.  
  112.                     /* Clear argument array */
  113.                     for (i = 0; i < MAX_ARGUMENTS; i++)
  114.                         {
  115.                             argv[i] = NULL;
  116.                         }
  117.  
  118.                     /* Parse command line arguments */
  119.                     if (GADS(aptr, alen, &xtra_help[0], &argv[0], &template[0]) < 0)
  120.                         {
  121.                             Puts(argv[0]);
  122.                         }
  123.                     else
  124.                         {
  125.                             struct RigidDiskBlock rdb;
  126.                             BPTR fh;
  127.                             BYTE *device, *file = argv[ARGUMENT_FILE];
  128.                             ULONG unit;
  129.                             USHORT error;
  130.  
  131.                             /* Install default arguments if not present */
  132.                             if (!argv[ARGUMENT_DEVICE])
  133.                                 {
  134.                                     argv[ARGUMENT_DEVICE] = DEFAULT_DEVICE;
  135.                                 }
  136.                             device = argv[ARGUMENT_DEVICE];
  137.                             if (!argv[ARGUMENT_UNIT])
  138.                                 {
  139.                                     argv[ARGUMENT_UNIT] = DEFAULT_UNIT;
  140.                                 }
  141.                             unit = Atol(argv[ARGUMENT_UNIT]);
  142.                             if (!argv[ARGUMENT_LOAD] && !argv[ARGUMENT_RESTORE] &&
  143.                                 !argv[ARGUMENT_DEFAULT] && !argv[ARGUMENT_REMOVE])
  144.                                 {
  145.                                     argv[ARGUMENT_LOAD] = (BYTE *) - 1L;
  146.                                 }
  147.                             if (!argv[ARGUMENT_SHOW] && !argv[ARGUMENT_SAVE] &&
  148.                                 !argv[ARGUMENT_BACKUP] && !argv[ARGUMENT_REMOVE])
  149.                                 {
  150.                                     argv[ARGUMENT_SHOW] = (BYTE *) - 1L;
  151.                                 }
  152.  
  153.                             /* Get output file handle for show */
  154.                             if (argv[ARGUMENT_SHOW])
  155.                                 {
  156.                                     BYTE *output = argv[ARGUMENT_OUTPUT];
  157.  
  158.                                     if (!output)
  159.                                         {
  160.                                             fh = Output();
  161.                                         }
  162.                                     else
  163.                                         {
  164.                                             if (!(fh = Open(output, (LONG) MODE_NEWFILE)))
  165.                                                 {
  166.                                                     Printf("Can't open '%s'\n", output);
  167.                                                     error = HBERR_FILE_OPEN_FAILED;
  168.                                                 }
  169.                                         }
  170.                                 }
  171.  
  172.                             /* Load hardblocks from device */
  173.                             if (!error && argv[ARGUMENT_LOAD])
  174.                                 {
  175.                                     Printf("Loading hardblocks from unit %ld of `%s'\n", unit,
  176.                                            device);
  177.                                     if (!(error = LoadHardBlocks(&rdb, device, unit)))
  178.                                         {
  179.  
  180.                                             /* Now play with hardblocks data */
  181.                                             if (argv[ARGUMENT_SHOW])
  182.                                                 {
  183.                                                     error = PrintHardBlocks(&rdb, fh);
  184.                                                 }
  185.                                             if (!error && argv[ARGUMENT_BACKUP])
  186.                                                 {
  187.                                                     if (!file)
  188.                                                         {
  189.                                                             Printf("No backup file name\n");
  190.                                                             error = HBERR_FILE_OPEN_FAILED;
  191.                                                         }
  192.                                                     else
  193.                                                         {
  194.                                                             Printf("Backup hardblocks to `%s'\n", file);
  195.                                                             error = BackupHardBlocks(&rdb, file);
  196.                                                         }
  197.                                                 }
  198.                                             FreeHardBlocks(&rdb);
  199.                                         }
  200.                                 }
  201.  
  202.                             /* Restore hardblocks from file */
  203.                             if (!error && argv[ARGUMENT_RESTORE])
  204.                                 {
  205.                                     if (!file)
  206.                                         {
  207.                                             Printf("No restore file name\n");
  208.                                             error = HBERR_FILE_OPEN_FAILED;
  209.                                         }
  210.                                     else
  211.                                         {
  212.                                             Printf("Restoring hardblocks from `%s'\n", file);
  213.                                             if (!(error = RestoreHardBlocks(&rdb, file)))
  214.                                                 {
  215.  
  216.                                                     /* Now play with hardblocks data */
  217.                                                     if (argv[ARGUMENT_SHOW])
  218.                                                         {
  219.                                                             error = PrintHardBlocks(&rdb, fh);
  220.                                                         }
  221.                                                     if (!error && argv[ARGUMENT_SAVE])
  222.                                                         {
  223.                                                             Printf("Saving hardblocks to unit %ld of"
  224.                                                                    " `%s'\n", unit, device);
  225.                                                             if (safety_check(device, unit) == TRUE)
  226.                                                                 {
  227.                                                                     error = SaveHardBlocks(&rdb, device, unit);
  228.                                                                 }
  229.                                                         }
  230.                                                     FreeHardBlocks(&rdb);
  231.                                                 }
  232.                                         }
  233.                                 }
  234.  
  235.                             /* Create standard rigid disk block */
  236.                             if (!error && argv[ARGUMENT_DEFAULT])
  237.                                 {
  238.                                     Printf("Creating standard rigid disk block for unit %ld"
  239.                                            " of `%s'\n", unit, device);
  240.                                     if (!(error = InitRigidDiskBlock(&rdb, device, unit)))
  241.                                         {
  242.  
  243.                                             /* Now play with hardblocks data */
  244.                                             if (argv[ARGUMENT_SHOW])
  245.                                                 {
  246.                                                     error = PrintHardBlocks(&rdb, fh);
  247.                                                 }
  248.                                             if (!error && argv[ARGUMENT_SAVE])
  249.                                                 {
  250.                                                     Printf("Saving hardblocks to unit %ld of `%s'\n", unit,
  251.                                                            device);
  252.                                                     if (safety_check(device, unit) == TRUE)
  253.                                                         {
  254.                                                             error = SaveHardBlocks(&rdb, device, unit);
  255.                                                         }
  256.                                                 }
  257.                                             if (!error && argv[ARGUMENT_BACKUP])
  258.                                                 {
  259.                                                     if (!file)
  260.                                                         {
  261.                                                             Printf("No backup file\n");
  262.                                                             error = HBERR_FILE_OPEN_FAILED;
  263.                                                         }
  264.                                                     else
  265.                                                         {
  266.                                                             Printf("Backup hardblocks to `%s'\n", file);
  267.                                                             error = BackupHardBlocks(&rdb, file);
  268.                                                         }
  269.                                                 }
  270.                                             FreeHardBlocks(&rdb);
  271.                                         }
  272.                                 }
  273.  
  274.                             /* Delete rigid disk block */
  275.                             if (!error && argv[ARGUMENT_REMOVE])
  276.                                 {
  277.                                     Printf("Removing hardblocks from unit %ld of `%s'\n", unit,
  278.                                            device);
  279.                                     if (safety_check(device, unit) == TRUE)
  280.                                         {
  281.                                             error = RemoveHardBlocks(device, unit);
  282.                                         }
  283.                                 }
  284.  
  285.                             /* Close output file handle opened for show */
  286.                             if (argv[ARGUMENT_SHOW] && argv[ARGUMENT_OUTPUT])
  287.                                 {
  288.                                     Close(fh);
  289.                                 }
  290.  
  291.                             /* Print error msg */
  292.                             if (error)
  293.                                 {
  294.                                     Printf("Error: primary=%d   secondary=%ld\n", error, IoErr());
  295.                                 }
  296.                             else
  297.                                 {
  298.                                     return_code = RETURN_OK;
  299.                                 }
  300.                         }
  301. #ifndef LINK_TEST
  302.                     CloseLibrary(HardBlocksBase);
  303.                 }
  304. #endif    /* LINK_TEST */
  305.             CloseLibrary(ArpBase);
  306.         }
  307.  
  308.     /* MANX crt0.asm forget to close DOS library, so we have to do it */
  309.     CloseLibrary(DOSBase);
  310.     return (return_code);
  311. }
  312.  
  313. /* Safety check before any writing to device unit */
  314.  
  315. STATIC BOOL
  316.  safety_check(BYTE * device, ULONG unit)
  317. {
  318.     BYTE buffer[MaxInputBuf];
  319.     BOOL answer = FALSE;
  320.  
  321.     Printf("DANGER: Do you really want to change hardblocks on unit %ld of\n"
  322.            "        device `%s' (YES|NO)? ", unit, device);
  323.     ReadLine(&buffer[0]);
  324.     if (strcmp(&buffer[0], "YES"))
  325.         {
  326.             Puts("Change aborted");
  327.         }
  328.     else
  329.         {
  330.             answer = TRUE;
  331.         }
  332.     return (answer);
  333. }
  334.